package com.ruoyi.experiment.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.ruoyi.experiment.domain.TeachingExperimentalEnvironmentExample;
import com.ruoyi.experiment.mapper.TeachingExperimentalEnvironmentExampleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.experiment.mapper.TeachingExperimentalEnvironmentMapper;
import com.ruoyi.experiment.domain.TeachingExperimentalEnvironment;
import com.ruoyi.experiment.service.ITeachingExperimentalEnvironmentService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 教学实验环境Service业务层处理
 * 
 * @author 邓鑫
 * @date 2023-07-29
 */
@Service
public class TeachingExperimentalEnvironmentServiceImpl implements ITeachingExperimentalEnvironmentService 
{
    @Resource
    private TeachingExperimentalEnvironmentMapper teachingExperimentalEnvironmentMapper;
    @Resource
    private TeachingExperimentalEnvironmentExampleMapper teachingExperimentalEnvironmentExampleMapper;

    /**
     * 查询教学实验环境
     * 
     * @param teachingEnvironmentId 教学实验环境主键
     * @return 教学实验环境
     */
    @Override
    public TeachingExperimentalEnvironment selectTeachingExperimentalEnvironmentByTeachingEnvironmentId(Long teachingEnvironmentId)
    {
        return teachingExperimentalEnvironmentMapper.selectTeachingExperimentalEnvironmentByTeachingEnvironmentId(teachingEnvironmentId);
    }

    /**
     * 查询教学实验环境列表
     * 
     * @param teachingExperimentalEnvironment 教学实验环境
     * @return 教学实验环境
     */
    @Override
    public List<TeachingExperimentalEnvironment> selectTeachingExperimentalEnvironmentList(TeachingExperimentalEnvironment teachingExperimentalEnvironment)
    {
        return teachingExperimentalEnvironmentMapper.selectTeachingExperimentalEnvironmentList(teachingExperimentalEnvironment);
    }

    /**
     * 新增教学实验环境
     * 
     * @param teachingExperimentalEnvironment 教学实验环境
     * @return 结果
     */
    @Override
    public int insertTeachingExperimentalEnvironment(TeachingExperimentalEnvironment teachingExperimentalEnvironment)
    {
        return teachingExperimentalEnvironmentMapper.insertTeachingExperimentalEnvironment(teachingExperimentalEnvironment);
    }

    /**
     * 修改教学实验环境
     * 
     * @param teachingExperimentalEnvironment 教学实验环境
     * @return 结果
     */
    @Override
    public int updateTeachingExperimentalEnvironment(TeachingExperimentalEnvironment teachingExperimentalEnvironment)
    {
        return teachingExperimentalEnvironmentMapper.updateTeachingExperimentalEnvironment(teachingExperimentalEnvironment);
    }

    /**
     * 批量删除教学实验环境
     * 
     * @param teachingEnvironmentIds 需要删除的教学实验环境主键
     * @return 结果
     */
    @Override
    public int deleteTeachingExperimentalEnvironmentByTeachingEnvironmentIds(Long[] teachingEnvironmentIds)
    {
        return teachingExperimentalEnvironmentMapper.deleteTeachingExperimentalEnvironmentByTeachingEnvironmentIds(teachingEnvironmentIds);
    }

    /**
     * 删除教学实验环境信息
     * 
     * @param teachingEnvironmentId 教学实验环境主键
     * @return 结果
     */
    @Override
    public int deleteTeachingExperimentalEnvironmentByTeachingEnvironmentId(Long teachingEnvironmentId)
    {
        return teachingExperimentalEnvironmentMapper.deleteTeachingExperimentalEnvironmentByTeachingEnvironmentId(teachingEnvironmentId);
    }

    /**
     * 生成环境实例
     */
    @Override
    @Transactional
    public void addExample(Long teachingEnvironmentId) {
        //获取环境对象
        TeachingExperimentalEnvironment tee = teachingExperimentalEnvironmentMapper.selectTeachingExperimentalEnvironmentByTeachingEnvironmentId(teachingEnvironmentId);
        TeachingExperimentalEnvironmentExample environmentExample=new TeachingExperimentalEnvironmentExample();
        environmentExample.setTeachingEnvironmentId(teachingEnvironmentId);
        environmentExample.setExampleCreateTime(LocalDateTime.now()); //设置创建时间
        environmentExample.setExampleEndTime(LocalDateTime.now().plusHours(tee.getTeachingEnvironmentTime())); //设置结束时间
        teachingExperimentalEnvironmentExampleMapper.insertTeachingExperimentalEnvironmentExample(environmentExample);
    }
}
